package components.product
{
	import components.rating.Rating;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.filters.DropShadowFilter;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import medias.InternalMedias;
	
	import spark.components.Label;
	import spark.core.SpriteVisualElement;
	
	import utils.ComponentUtils;
	
	import vo.ProductVO;

	public class ProductSprite extends SpriteVisualElement
	{
		private var _image:Sprite;
		
		private var _title:TextField;
		
		private var _price:TextField;
		
		private var rating:Rating;
		
		private var _data:ProductVO;
		
		private var _created:Boolean = false;
		
		private const TITLE_TEXT_FORMAT:TextFormat = new TextFormat('Arial',14,0x555555,true);
		
		private const PRICE_TEXT_FORMAT:TextFormat = new TextFormat('Arial',14,0x555555);
		
		private const FREE_TEXT_FORMAT:TextFormat = new TextFormat('Arail',14,0xb3c833);
		
		private var IMAGE_HEIGHT:int;
		
		private var IMAGE_WIDTH:int;
		
		public function ProductSprite()
		{
			super();
		}
		
		public function set data(pValue:ProductVO):void
		{
			if(pValue != _data && pValue)
			{
				_data = pValue;
				
				if(!_created)
				{
					createChildren();
				}
				else
				{
					updateDisplay();
				}
			}
		}
		
		public function createChildren():void
		{
			_created = true;
			
			IMAGE_HEIGHT = height * 0.66;
			IMAGE_WIDTH = width - 20;
			
			filters = [new DropShadowFilter(2,45,0,0.3)];
			
			ComponentUtils.drawRectangle(graphics,0,0,width,height,0xffffff);
			
			var pX:int = 10;
			var pY:int = 10;
			
			_image = new Sprite();
			_image.x = pX;
			_image.y = pY;
			
			if(!_data.mainMedia)
			{
				_image.addChild(new InternalMedias.NO_IMAGE() as DisplayObject);
				_image.height = IMAGE_HEIGHT;
				_image.width = IMAGE_WIDTH;
				addChild(_image);
			}
			else
			{
				loadImage();
			}
			
			
			
			pX = 5;
			pY += IMAGE_HEIGHT + 5;
			
			_title = new TextField();
			
			if(_data.label)
				_title.text = _data.label;
			else
				_title.text = 'Title...';
				
			_title.width = this.width - 10;
			_title.multiline = false;
			_title.x = pX;
			_title.y = pY;
			_title.setTextFormat(TITLE_TEXT_FORMAT);
			
			addChild(_title);
			
			pY += 20;
			
			_price = new TextField();
			_price.x = pX;
			_price.y = pY;
			_price.width = this.width;
			if(_data.price == 0)
			{
				_price.text = 'GRATUIT';
				_price.setTextFormat(FREE_TEXT_FORMAT);
			}
			else
			{
				_price.text = _data.price + '€';
				_price.setTextFormat(PRICE_TEXT_FORMAT);
			}
			
			addChild(_price);
			
			pY += 20;
			
			rating = new Rating();
			rating.x = pX;
			rating.y = pY;
			rating.width = 95;
			rating.showSelectedValue = false;
			rating.liveRollOver = false;
			rating.value = _data.note;
			addChild(rating);
		}
		
		public function updateDisplay():void
		{
			
		}
		
		private var imgLoader:Loader;
		
		private function loadImage():void
		{
			imgLoader = new Loader();
			var request:URLRequest = new URLRequest('http://localhost/cultu/'+_data.mainMedia.url);
			imgLoader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS,onLoadingComplete,false,0,true);
			imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onLoadingFailed,false,0,true);
			imgLoader.load(request);
		}
		
		private function onLoadingComplete(pEvent:HTTPStatusEvent):void
		{
			if(_image.numChildren > 0)
			{
				_image.removeChildren();
			}
			
			switch (pEvent.status)
			{
				case 404: // Image not found.
					_image.addChild(new InternalMedias.NO_IMAGE() as DisplayObject);
					break;
				case 200: // Image found and loaded
					if(imgLoader.content)
						_image.addChild(imgLoader.content);
					else
						_image.addChild(new InternalMedias.NO_IMAGE() as DisplayObject);
					break;
				default:
					_image.addChild(new InternalMedias.NO_IMAGE() as DisplayObject);
					break;
			}
			addChild(_image);
			_image.height = IMAGE_HEIGHT;
			_image.width = IMAGE_WIDTH;
		}
		
		private function onLoadingFailed(pEvent:IOErrorEvent):void
		{
			if(_image.numChildren > 0)
			{
				_image.removeChildren();
			}
			addChild(_image);
			_image.addChild(new InternalMedias.NO_IMAGE() as DisplayObject);
			_image.height = IMAGE_HEIGHT;
			_image.width = IMAGE_WIDTH;
		}
	}
}